package com.jdym.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdym.R;
import com.jdym.entity.Certify;
import com.jdym.entity.Firm;
import com.jdym.entity.Integral;
import com.jdym.service.CertifyService;
import com.jdym.service.FirmService;
import com.jdym.service.IntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@Controller
@RequestMapping("/firm")
public class FirmController {
    @Autowired
    FirmService firmService;
    @Autowired
    CertifyService certifyService;
    @Autowired
    IntegralService integralService;
//企业信息管理
    @ResponseBody
    @GetMapping("/firmInfoManage/{id}")
    public R firmInfoManage(@PathVariable int id){
        Firm firmInfo=firmService.getBaseMapper().selectById(id);
        Certify certify=certifyService.getBaseMapper().selectOne(new QueryWrapper<Certify>().eq("firm_id",id));
        return R.ok().data("firmInfo",firmInfo).data("certify",certify);
    }
    @ResponseBody
    @RequestMapping("/updateTel")
    public R updateTel(@RequestParam int firmId,
                       @RequestParam(required = false) String tel,
                       @RequestParam(required = false)  String mail){
        Firm firm=new Firm();
        if(tel !=null){
            firm.setFirmId(firmId);
            firm.setFirmTel(tel);
            firmService.getBaseMapper().updateById(firm);
            return R.ok().message("更新成功");
        }
        else if (mail!=null){
            firm.setFirmId(firmId);
            firm.setFirmEmail(mail);
            firmService.getBaseMapper().updateById(firm);
            return R.ok().message("更新成功");
        }
        else {
            return R.error().message("更新失败");
        }
    }

    //    注册跳转
    @RequestMapping("/toRegister")
    private String toRegister(){
        return "register";
    }
    //    注册用户信息
    @RequestMapping("/insert")
    private String insertFirm(Firm firm){
        firmService.getBaseMapper().insert(firm);
        Integral integral=new Integral();
        integral.setFirmid(firm.getFirmId());
        integral.setIntegralval(30);
        integralService.getBaseMapper().insert(integral);
        return "entry";
    }
    //用户登录(查询用户名和密码)
    @RequestMapping(value = "/login32",method =RequestMethod.POST)
    @ResponseBody
    public R Login2(HttpServletRequest request, Firm firm,HttpSession session){
        System.out.println("Firm=="+firm);
        Firm f= firmService.selectLogin(firm.getFirmName(),firm.getFirmPassword());
        if (f!=null){
            //前台接受登录的用户名
            request.getSession().setAttribute("firmName",f.getFirmName());
            session.setAttribute("firmName",f.getFirmName());
            session.setAttribute("currentuser",f);

//            request.getSession().setAttribute("currentuser",f);
            System.out.println("----------------currentuser------------------"+f);
            return R.ok().data("f",f);
        }
        return R.error().data("msg","");
    }

    //    密码找回，密码重置
    @RequestMapping("/update_pwd")
    private String update_pwd(Integer firmId){
        System.out.println("================================="+firmId);
        firmService.getBaseMapper().selectById(firmId);
        return "find_pwd";

    }
    //修改保存密码
    @RequestMapping("/update_save_pwd")
    private String update_save_pwd(Firm firm){
        firmService.getBaseMapper().updateById(firm);
        System.out.println("======================"+firm);
        return "redirect:/firm/insert";
    }

    @RequestMapping("/getPwd")
    public String getPwd(String firmName, String firmPassword)
    {
        System.out.println("1111"+firmPassword);
        System.out.println();
        Firm firm= firmService.getBaseMapper().selectOne(new QueryWrapper<Firm>().eq("firm_name",firmName));
        int id=firm.getFirmId();
        Firm firm1=new Firm();
        firm1.setFirmId(id);
        firm1.setFirmPassword(firmPassword);
        firmService.getBaseMapper().updateById(firm1);
        return "entry";

    }

    //    退出登录
    @RequestMapping("/removeFirm")
    public  String removeFirm(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("firmName");
        return "entry";

    }
}

